package com.vehicle.repair.controller;

import com.vehicle.repair.model.WorkOrder;
import com.vehicle.repair.service.WorkOrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/orders")
public class WorkOrderController {
    private final WorkOrderService workOrderService;

    public WorkOrderController(WorkOrderService workOrderService) {
        this.workOrderService = workOrderService;
    }

    @PostMapping
    public ResponseEntity<WorkOrder> createWorkOrder(@RequestBody WorkOrder workOrder,
                                                   @RequestParam Integer userId,
                                                   @RequestParam Integer vehicleId) {
        try {
            WorkOrder createdOrder = workOrderService.createWorkOrder(workOrder, userId, vehicleId);
            return ResponseEntity.ok(createdOrder);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<WorkOrder> getWorkOrderById(@PathVariable Integer id) {
        Optional<WorkOrder> order = workOrderService.getWorkOrderById(id);
        return order.map(ResponseEntity::ok)
                   .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<WorkOrder>> getWorkOrdersByUser(@PathVariable Integer userId) {
        try {
            List<WorkOrder> orders = workOrderService.getWorkOrdersByUser(userId);
            return ResponseEntity.ok(orders);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<List<WorkOrder>> getWorkOrdersByStatus(@PathVariable String status) {
        try {
            WorkOrder.OrderStatus orderStatus = WorkOrder.OrderStatus.valueOf(status);
            List<WorkOrder> orders = workOrderService.getWorkOrdersByStatus(orderStatus);
            return ResponseEntity.ok(orders);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{id}/status")
    public ResponseEntity<WorkOrder> updateWorkOrderStatus(@PathVariable Integer id,
                                                        @RequestParam String status) {
        try {
            WorkOrder.OrderStatus orderStatus = WorkOrder.OrderStatus.valueOf(status);
            WorkOrder updatedOrder = workOrderService.updateWorkOrderStatus(id, orderStatus);
            return ResponseEntity.ok(updatedOrder);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteWorkOrder(@PathVariable Integer id) {
        workOrderService.deleteWorkOrder(id);
        return ResponseEntity.noContent().build();
    }
}
